package com.example.aop;

import com.example.anno.LogAnno;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

@Slf4j
@Aspect
@Component
public class LogAnnoAspect {

    @Pointcut("@annotation(com.example.anno.LogAnno)")
    public void pointcut(){}

    @Around(value = "pointcut()")
    public Object logAnno(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        LogAnno logAnno = method.getAnnotation(LogAnno.class);
        Parameter[] parameters = method.getParameters();
        Object[] args = pjp.getArgs();
        for (int i = 0; i < parameters.length; i++) {
            log.info("param: {}, value: {}", parameters[i].getName(), args[i]);
        }
        return pjp.proceed();
    }
}
